﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace MediaWiki.Office.Model
{
  public class Fonts
  {
    private string _ascii;
    private string _asciiTheme;
    private string _highAnsi;
    private string _highAnsiTheme;
    private string _eastAsian;
    private string _eastAsianTheme;
    private string _complexScript;
    private string _complexScriptTheme;
    private RunProperties _runProperties;

    public string Ascii
    {
      get { return _runProperties.GetValue(p => ResolveThemeFont(p.Fonts._asciiTheme) ?? p.Fonts._ascii); }
      set { _ascii = value; }
    }

    public string AsciiTheme
    {
      get { return _runProperties.GetValue(p => p.Fonts._asciiTheme); }
      set { _asciiTheme = value; }
    }

    public string HighAnsi
    {
      get { return _runProperties.GetValue(p => ResolveThemeFont(p.Fonts._highAnsiTheme) ?? p.Fonts._highAnsi); }
      set { _highAnsi = value; }
    }

    public string HighAnsiTheme
    {
      get { return _runProperties.GetValue(p => p.Fonts._highAnsiTheme); }
      set { _highAnsiTheme = value; }
    }

    public string EastAsian
    {
      get { return _runProperties.GetValue(p => ResolveThemeFont(p.Fonts._eastAsianTheme) ?? p.Fonts._eastAsian); }
      set { _eastAsian = value; }
    }

    public string EastAsianTheme
    {
      get { return _runProperties.GetValue(p => p.Fonts._eastAsianTheme); }
      set { _eastAsianTheme = value; }
    }

    public string ComplexScript
    {
      get { return _runProperties.GetValue(p => ResolveThemeFont(p.Fonts._complexScriptTheme) ?? p.Fonts._complexScript); }
      set { _complexScript = value; }
    }

    public string ComplexScriptTheme
    {
      get { return _runProperties.GetValue(p => p.Fonts._complexScriptTheme); }
      set { _complexScriptTheme = value; }
    }

    public Fonts(RunProperties runProperties, XElement fontElement)
    {
      _runProperties = runProperties;

      if (fontElement == null)
        return;

      XNamespace ns = fontElement.Name.Namespace;

      _ascii = (string)fontElement.Attribute(ns + "ascii");
      _asciiTheme = (string)fontElement.Attribute(ns + "asciiTheme");
      _highAnsi = (string)fontElement.Attribute(ns + "hAnsi");
      _highAnsiTheme = (string)fontElement.Attribute(ns + "hAnsiTheme");
      _complexScript = (string)fontElement.Attribute(ns + "cs");
      _complexScriptTheme = (string)fontElement.Attribute(ns + "csTheme");
      _eastAsian = (string)fontElement.Attribute(ns + "eastAsia");
      _eastAsianTheme = (string)fontElement.Attribute(ns + "eastAsiaTheme");
    }

    private string ResolveThemeFont(string themeFont)
    {
      if (themeFont == null)
        return null;

      Content run = _runProperties.Content;
      if (run == null)
        throw new InvalidOperationException("Cannot resolve theme font. No run associated with run properties.");
      
      Document mainDocument = run.MainDocument;
      if (mainDocument == null)
        throw new InvalidOperationException("Cannot resolve theme font. No document associated with run.");

      switch (themeFont)
      {
        case "minorHAnsi":
          return _runProperties.Content.MainDocument.ThemeMinorFont;
        case "majorHAnsi":
          return _runProperties.Content.MainDocument.ThemeMajorFont;
        default:
          throw new ArgumentException("Unexpected theme font: " + themeFont, "themeFont");
      }
    }
  }
}